﻿
@{
    ViewBag.Title = "Patient Index";
}

@Html.Partial("_UserPage")
<div id="patient" class="row result">
    <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 sidebar user-sidebar">
        <ul class="nav nav-sidebar">
            <li id="UserProfile" class="active">Profile</li>
            <li id="UserVisitHistory">Visit History</li>
        </ul>
    </div>
    <div id="main" class="col-xs-9 col-sm-9 col-md-10 col-lg-10 main user-content">
        @Html.Partial("_UserProfile")
        @Html.Partial("~/Views/Patient/ListOfVisitForPatient.cshtml")
    </div>
</div>
@Html.Partial("_Message")
@Html.Partial("~/Views/Patient/ViewVisitForm.cshtml")
@Html.Partial("_PrintVisitForm")
@Html.Partial("~/Views/Patient/ChangePassword.cshtml")
@section Scripts {
    <script>
        var userType = '@User.UserType';
    </script>
    @Scripts.Render("~/bundles/message")
    @Scripts.Render("~/Scripts/BodyController.js")
    @Scripts.Render("~/Scripts/ContentHeightController.js")
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/my_knockout")
    @Scripts.Render("~/Scripts/ViewModel/PatientVM.js")
    @Scripts.Render("~/Scripts/ViewModel/VisitViewVM.js")
    @Scripts.Render("~/Scripts/jquery.printArea.js")
    @Scripts.Render("~/Scripts/ViewModel/VisitPrintVM.js")
    @Scripts.Render("~/Scripts/ViewModel/ChangePasswordVM.js")
    <script>
        $(function () {
            ko.applyBindings(patientVM, document.getElementById("patient"));
            ko.applyBindings(visitViewVM, document.getElementById("viewVisitVM"));
            ko.applyBindings(visitPrintVM, document.getElementById("printVisitVM"));
            ko.applyBindings(changePasswordVM, document.getElementById("changePasswordVM"));
            var indexController = {
                message: messageController,
                body: bodyController,
                listVisit: $("#listVisit"),
                viewVisitForm: $("#viewVisitForm"),
                displayViewVisitForm: function (state) {
                    if (state == true) {
                        this.viewVisitForm.show();
                        //this.viewVisitForm.css({
                        //    "display": "block"
                        //});
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.viewVisitForm.hide();
                        //this.viewVisitForm.css({
                        //    "display": "none"
                        //});
                        this.body.displayBodyScroll(true);
                        visitViewVM.cancelLoadRequest();
                    }
                },

                btnFirstVisitPage: $("#firstVisitPage"),
                btnPreviousVisitPage: $("#previousVisitPage"),
                enableVisitPreviousGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnFirstVisitPage.on("click", function () {
                            patientVM.loadVisitsPage(indexController, 1);
                        });
                        self.btnFirstVisitPage.removeClass("disabled");
                        self.btnPreviousVisitPage.on("click", function () {
                            patientVM.loadVisitsPage(indexController, patientVM.VisitsPage() - 1);
                        });
                        self.btnPreviousVisitPage.removeClass("disabled");
                    }
                    else {
                        self.btnFirstVisitPage.off("click");
                        self.btnFirstVisitPage.addClass("disabled");
                        self.btnPreviousVisitPage.off("click");
                        self.btnPreviousVisitPage.addClass("disabled");
                    }
                },

                btnLastVisitPage: $("#lastVisitPage"),
                btnNextVisitPage: $("#nextVisitPage"),
                enableVisitNextGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnLastVisitPage.on("click", function () {
                            patientVM.loadVisitsPage(indexController, patientVM.VisitsPageCount());
                        });
                        self.btnLastVisitPage.removeClass("disabled");
                        self.btnNextVisitPage.on("click", function () {
                            patientVM.loadVisitsPage(indexController, patientVM.VisitsPage() + 1);
                        });
                        self.btnNextVisitPage.removeClass("disabled");
                    }
                    else {
                        self.btnLastVisitPage.off("click");
                        self.btnLastVisitPage.addClass("disabled");
                        self.btnNextVisitPage.off("click");
                        self.btnNextVisitPage.addClass("disabled");
                    }
                },

                enableListVisit: function (state) {
                    var self = this;
                    if (state == true) {
                        self.listVisit.on("click", "tr", function (event) {
                            visitViewVM.loadVisit($(event.currentTarget).find("td").eq(0).text(), indexController);
                            self.displayViewVisitForm(true);
                        });
                        $("#view-visit-close").on("click", function () {
                            self.displayViewVisitForm(false);
                        });

                        self.listVisit.on("click", ".print", function (event) {
                            visitPrintVM.loadVisit($(event.currentTarget).parent("tr").find("td").eq(0).text(), indexController);
                            self.displayPrintVisitForm(true);
                            event.stopPropagation();
                        });
                        self.enableBtnPrintVisitClose(true);
                        self.enableBtnChangePasword(true);
                        self.enableBtnChangePasswordCancel(true);
                    }
                    else {
                        this.listVisit.off("click");
                        $("#view-visit-close").off("click");
                        self.enableBtnPrintVisitClose(false);
                        self.enableBtnChangePasword(false);
                        self.enableBtnChangePasswordCancel(false);
                    }
                },

                printVisitForm: $("#printVisitForm"),
                displayPrintVisitForm: function (state) {
                    if (state == true) {
                        this.printVisitForm.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.printVisitForm.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },

                printerLoading: function () {
                    indexController.message.processing("printer_loading");
                    indexController.message.processComplete("printer_wait");
                },
                printVisit: function () {
                    $("#printContent").printArea();
                },

                btnPrintVisitPrint: $("#print-visit-print"),
                enableBtnPrintVisitPrint: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnPrintVisitPrint.on("click", function () {
                            self.printVisit();
                            self.printerLoading();
                        });
                    }
                    else {
                        this.btnPrintVisitPrint.off("click");
                    }
                },

                btnPrintVisitClose: $("#print-visit-close"),
                enableBtnPrintVisitClose: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnPrintVisitClose.on("click", function () {
                            self.displayPrintVisitForm(false);
                            visitPrintVM.cancelLoadRequest();
                        });
                    }
                    else {
                        this.btnPrintVisitClose.off("click");
                    }
                },

                UserVisitHistory: $("#UserVisitHistory"),
                UserProfile: $("#UserProfile"),
                UserVisitHistoryForm: $("#UserVisitHistoryForm"),
                UserProfileForm: $("#UserProfileForm"),
                activeSideBar: function () {
                    var p = this;
                    p.UserProfile.on("click", function () {
                        if (!p.UserProfileForm.is(":visible")) {
                            p.UserProfileForm.show();
                            p.UserVisitHistoryForm.hide();
                            p.UserProfile.addClass("active");
                            p.UserVisitHistory.removeClass("active");
                        }
                    });
                    p.UserVisitHistory.on("click", function () {
                        if (!p.UserVisitHistoryForm.is(":visible")) {
                            p.UserProfileForm.hide();
                            p.UserVisitHistoryForm.show();
                            p.UserProfile.removeClass("active");
                            p.UserVisitHistory.addClass("active");
                        }
                    });
                },

                btnChangePassword: $("#change-password"),
                enableBtnChangePasword: function (state) {
                    var p = this;
                    if (state) {
                        p.btnChangePassword.on("click", function () {
                            changePasswordVM.clearOldData();
                            p.displayChangePasswordForm(true);
                        });
                        p.btnChangePassword.removeClass("disabled");
                    }
                    else {
                        p.btnChangePassword.off("click");
                        p.btnChangePassword.addClass("disabled");
                    }
                },
                ChangePasswordForm: $("#ChangePasswordForm"),
                btnChangePasswordSave: $("#change-password-save"),
                btnChangePasswordCancel: $("#change-password-cancel"),
                enableBtnChangePasswordSave: function (state) {
                    if (state == true) {
                        this.btnChangePasswordSave.on("click", function () {
                            changePasswordVM.savePassword(indexController);
                        });
                    }
                    else {
                        this.btnChangePasswordSave.off("click");
                    }
                },
                enableBtnChangePasswordCancel: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnChangePasswordCancel.on("click", function () {
                            self.displayChangePasswordForm(false);
                        });
                    }
                    else {
                        self.btnChangePasswordCancel.off("click");
                    }
                },
                displayChangePasswordForm: function (state) {
                    if (state) {
                        //var inputs = this.ChangePasswordForm.find("input");
                        //inputs.val("");
                        this.ChangePasswordForm.show();
                        $("#CurrPass").focus();
                        this.enableBtnChangePasswordSave(true);
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.ChangePasswordForm.hide();
                        this.enableBtnChangePasswordSave(false);
                        this.body.displayBodyScroll(true);
                    }
                }

            };
            patientVM.loadPatientInfo(indexController);
            contentHeightController.setContentHeight();
            contentHeightController.activeChangeContentHeight();
            indexController.activeSideBar();
            indexController.enableVisitPreviousGroup(false);
            indexController.enableVisitNextGroup(false);
        });
    </script>
}
